package com.test.timmy;

import java.util.Iterator;

import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class MainGrid
{
	public void draw(final Activity activity)
	{
		//using Table instead of grid to keep compatibility to Api Lv10
		TableLayout table = new TableLayout(activity);
        table.setStretchAllColumns(true);
        table.setShrinkAllColumns(true);
        TableRow currentRow = new TableRow(activity);
        currentRow.setGravity(Gravity.CENTER);
        table.addView(currentRow);

		final ViewGroup contentView = (ViewGroup) activity.findViewById(R.id.main_layout);

		Iterator<Person> iterator = Controller.getInstance().getPeople().values().iterator();
		while(iterator.hasNext())
		{
			final Person person = iterator.next();
			ImageView myImage = new ImageView(activity);
			myImage.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(activity.getApplicationContext(), PersonDetails.class);
					intent.putExtra(Person.TAG_PERSON_ID, person.getUniqueID());
					//for back button support
					activity.startActivity(intent);
				}
			});
			myImage.setImageBitmap(person.getPhoto());

			currentRow.addView(myImage);
			if (currentRow.getChildCount() >= 5)
			{
		        currentRow = new TableRow(activity);
		        currentRow.setGravity(Gravity.CENTER);
		        table.addView(currentRow);
			}
		}

		contentView.addView(table);
	}
}
